package com.mioto.ld.module.personal.controller;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.module.personal.model.UserInfoDTO;
import com.mioto.ld.module.personal.model.UserInfoVO;
import org.springframework.web.bind.annotation.*;
import com.mioto.ld.module.personal.model.User;
import com.mioto.ld.module.personal.service.IUserService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.exception.BasicException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 *
 * @author mioto
 * @date 2024-05-09 09:30:37
 */
@RestController
@RequestMapping("user")
@Api(tags = "用户信息管理")
public class UserController {
    @Resource
    private IUserService userService;

    @GetMapping
    @ApiOperation(value="查询用户信息列表",response = User.class)
    public ResultData list (HttpServletRequest request,User user){
        return ResultData.success(userService.findList(user));
    }

    @GetMapping("/pager")
    @ApiOperation(value="分页查询用户信息",response = User.class)
    public ResultData pager (HttpServletRequest request, User user,BasePager basePager){
        PageHelper.startPage(basePager.getPage(), basePager.getRows(), basePager.getSortBy());
        List<UserInfoVO> list = userService.findList(user);
        PageInfo<UserInfoVO> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return ResultData.success(result);
    }

    @GetMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名查询用户信息",response = User.class)
    public ResultData findByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return ResultData.success(userService.findByColumn(StrUtil.toUnderlineCase(column),value));
    }

    @PostMapping
    @ApiOperation(value="新增用户信息")
    public ResultData add (UserInfoDTO user){
        return Optional.of(userService.insert(user))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(user))
                .orElseThrow(() -> new BasicException(SystemTip.INSERT_FAIL));
    }

    @PatchMapping
    @ApiOperation(value="更新用户信息 局部更新,忽略空值")
    public ResultData updateIgnoreNull (User user){
        return Optional.of(userService.updateIgnoreNull(user))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(user))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @PutMapping
    @ApiOperation(value="更新用户信息 整体更新")
    public ResultData update (User user){
        return Optional.of(userService.update(user))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(user))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @DeleteMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名删除用户信息")
    public ResultData deleteByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return Optional.of(userService.deleteByColumn(StrUtil.toUnderlineCase(column),value))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @DeleteMapping("/batch")
    @ApiOperation(value="批量删除用户信息")
    public ResultData batchDelete (Integer... ids){
        return Optional.of(userService.batchDelete(ids))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @PostMapping("/updatePwd")
    @ApiOperation(value="修改密码")
    public ResultData updatePwd(int userId,String oldPwd,String newPwd){
        return Optional.of(userService.updatePwd(userId,oldPwd,newPwd))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }
}